<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<!-- 通过config接口注入权限验证配置 -->
<script src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>

<!-- 通过agentConfig注入应用的权限 -->
<script src="https://open.work.weixin.qq.com/wwopen/js/jwxwork-1.0.0.js"></script>

<script src="https://wwcdn.weixin.qq.com/node/open/js/wecom-jssdk-2.0.2.js"></script>

<head>
    <title>测试获取用户ID</title>
    <meta charset="UTF-8">
    <style>
    </style>
</head>
<body>
    <div class="container">
        <h2>INFO:</h2>
        <h2 id="info"></h2>
        <h2 id="userId">外部联系人ID：</h2>
        <button onclick="sendChatMessage()">分享消息到当前会话</button>
        <button onclick="shareToExternalContact()">群发消息给客户</button>
        <button onclick="shareToExternalMoments()">发表内容到客户朋友圈</button>
    </div>
    
    <script th:inline="javascript">
        const configCorp = {
            timestamp: /*[[${configCorp.timestamp}]]*/ '',
            nonceStr: /*[[${configCorp.nonceStr}]]*/ '',
            signature: /*[[${configCorp.signature}]]*/ ''
        };
        const config = {
            appId: /*[[${config.appId}]]*/ '',
            timestamp: /*[[${config.timestamp}]]*/ '',
            nonceStr: /*[[${config.nonceStr}]]*/ '',
            signature: /*[[${config.signature}]]*/ ''
        };
        
        // 获取当前会话的ExternalUserId
        //setTimeout(checkWxReady, 500); 
        
        
        setTimeout(register, 500);
        
        function register(){

   		 document.getElementById('info').innerHTML = ("register开始..." + ww.register);
        	try{
	        	ww.register({
	        		  corpId: 'ww669ca452439131bc',       // 必填，当前用户企业所属企业ID
	        		  agentId: 1000008,                  // 必填，当前应用的AgentID
	        		  jsApiList: ['getContext', 'sendChatMessage', 'shareToExternalContact', 'shareToExternalMoments'], // 必填，需要使用的JSAPI列表
	        		  //getConfigSignature,                // 必填，根据url生成企业签名的回调函数
	        		  getAgentConfigSignature,            // 必填，根据url生成应用签名的回调函数
	        		  onConfigSuccess: function(res){
				            document.getElementById('info').innerHTML = "register onConfigSuccess:" + JSON.stringify(res);
		        		},
		        		onConfigFail: function(res){
				            document.getElementById('info').innerHTML = "register onConfigFail:" + JSON.stringify(res) ;
		        		},
		        		onConfigComplete: function(res){
				            document.getElementById('info').innerHTML = "register onConfigComplete:" + JSON.stringify(res) ;
		        		},
		        		onAgentConfigFail: function (res){
				            document.getElementById('info').innerHTML = "register onAgentConfigFail:" + JSON.stringify(res) ;
		        		},
		        		onAgentConfigSuccess: function (res){
				            document.getElementById('info').innerHTML = "register onAgentConfigSuccess:" + JSON.stringify(res) ;
		        		},
		        		onAgentConfigFail: function (res){
				            document.getElementById('info').innerHTML = "register onAgentConfigFail:" + JSON.stringify(res) ;
		        		},
		        		onAgentConfigComplete: function (res){
				            document.getElementById('info').innerHTML = "register onAgentConfigComplete:" + JSON.stringify(res) ;
		        		}
	        		});
	        	
	        	
        	}catch(e){
        		 document.getElementById('info').innerHTML = ("register发生异常：" + e);
        	}
        	

            //setTimeout(getContext, 3000);
        }
        
        function getConfigSignature(url) {
        	  // 根据 url 生成企业签名
        	  // 生成方法参考 https://developer.work.weixin.qq.com/document/14924
       	  	return configCorp;
       	}
        function getAgentConfigSignature(url) {
       	  // 根据 url 生成应用签名，生成方法同上，但需要使用应用的 jsapi_ticket
     	  	return config;
       	}
        
        function getContext(){
        	document.getElementById('info').innerHTML = ("开始执行22getContext...");
        	try{
	        	ww.getContext({
	        		success: function(res){
			            document.getElementById('info').innerHTML = "入口类型：" + res.entry;
	        		},
	        		fail: function(res){
			            document.getElementById('info').innerHTML = "getContext发生错误" + JSON.stringify(res) ;
	        		}
	        		
	        	});
        	} catch (e){
	            document.getElementById('info').innerHTML = ("getContex发生异常：" + e);
        	}
        }
        
        // 分享消息到当前会话
        function sendChatMessage(){
            document.getElementById('info').innerHTML = "分享消息到当前会话：";
            try{
		       	ww.sendChatMessage({
		       		  msgtype: 'text',
		       		  text: {
		       		    content: '你好111'
		       		  },
	        		success: function(res){
			            document.getElementById('info').innerHTML = "sendChatMessage成功：" + res;
	        		},
	        		fail: function(res){
			            document.getElementById('info').innerHTML = "sendChatMessage发生错误" + JSON.stringify(res) ;
	        		}
		       		})
            } catch (e){
	            document.getElementById('info').innerHTML = ("sendChatMessage发生异常：" + e);
            }
        }
        
        // 群发消息给客户
        function shareToExternalContact(){
            try{
            	ww.shareToExternalContact({
            		  externalUserIds: ["wmFibqCgAABJ0wcmZh5lM4EjFvUnjWJA","wmFibqCgAAKJoyRahfbGpmU4h4-cy1AA"],
            		  text: {
            		    content: '企业微信'
            		  },
            		  attachments: [
            		    {
            		      msgtype: 'image',
            		      image: {
            		        imgUrl: 'https://mall-htiannet.oss-cn-hangzhou.aliyuncs.com/image/2023/02/28/1677576263540.png'
            		      }
            		    }
            		  ]
            		})
            } catch (e){
	            document.getElementById('info').innerHTML = ("shareToExternalContact发生异常：" + e);
            }
        }
        
        // 发表内容到客户朋友圈
        function shareToExternalMoments(){

            try{
            	ww.shareToExternalMoments({
            		  text: {
            		    content: '企业微信'
            		  },
            		  attachments: [
            		    {
            		      msgtype: 'image',
            		      image: {
            		        imgUrl: 'https://res.mail.qq.com/node/ww/wwmng/style/images/index_share_logo$13c64306.png'
            		      }
            		    }
            		  ]
            		})
            } catch (e){
	            document.getElementById('info').innerHTML = ("shareToExternalMoments发生异常：" + e);
            }
        }
    </script>
</body>
</html>